// miniprogram/pages/register/Index.js
const app = getApp()
var util = require('../util.js')
Page({

  /**
   * 页面的初始数据
   */

  data: {
    array: ['男', '女'],
    index: 2,
    items: [
    ],
    zalue: '',
    disabled: false,
    btnstate: "default",
    btnname: "确认提交",
    btnstate: false
  },
  pickSex: function (e) {
    this.setData({
      index: e.detail.value
    });
  },
  getdate: function () {
    var that = this;
    let data = '{"[]":{"K_W_Dict":{"Pid":"position","@column":"name"}}}';
    wx.cloud.callFunction({
      name: 'Get_fun',
      data: {
        data: data
      },
      success: res => {
        try {
          let obj = JSON.parse(res.result)["[]"];

          for (let i = 0; i < obj.length; i++) {
            var str = '{"name":"' + obj[i].K_W_Dict.name + '","value":"' + obj[i].K_W_Dict.name + '"}';
            console.log(str);
            that.setData({
              items: this.data.items.concat(JSON.parse(str))

            });
          }
        } catch (error) {
          wx.showToast({
            icon: 'none',
            title: '当前没有数据',
          })
        }
      },
      fail: err => {
        console.error('[云函数] [login] 调用失败', err);
        wx.showToast({
          icon: 'none',
          title: '数据调用失败，请联系管理员',
        })
      }
    })

  },

  click: function (e) {
    var id = e.currentTarget.dataset.id
    var that = this
    that.setData({
      id: id
    })
  },
  radioChange: function (e) {
    this.setData({
      zalue: e.detail.value
    })
  },
  toggleDialog() {
    this.setData({
      showDialog: !this.data.showDialog
    });
  },
  freeBack: function () {
    var that = this
    if (this.data.value == 'show') {
      wx.showModal({
        title: '提示',
        content: '你没有选择任何内容',
      })
    }
    that.setData({
      showDialog: !this.data.showDialog
    })
  },
  freetoBack: function () {
    var that = this
    wx.showModal({
      title: '提示',
      content: '你没有选择任何内容',
    })
    that.setData({
      showDialog: !this.data.showDialog,
      value: 'show',
      checked: false,
    })
  },
  /***表单提交***/
  formSubmit: function (e) {
    let sex = this.data.array[this.data.index];
    let { userName, mobile, zw, add } = e.detail.value;
    if (!userName || !mobile || !add) {
      wx.showToast({
        icon: 'none',
        title: '请输入相关信息！',
      });
      return;
    }
    this.setData({
      btnname: "提交中",
      btnstate: true
    })
    let data = '{"K_W_User": {"Openid":"' + getApp().globalData.openid + '","Name":"' + userName + '","Telno":"' + mobile + '","ADD":"' + add + '","Sex":"' + sex + '","TypeName":"' + zw + '","Creatorid":"' + getApp().globalData.appid + '"}}';
    wx.cloud.callFunction({
      name: 'Add_fun',
      data: {
        data: util.encode(util.encodeUTF8(data))
      },
      success: res => {
        wx.showModal({
          title: '注册成功',
          content: '请等待审核。。',
          showCancel: false,
        })
        wx.redirectTo({
          url: "../main/Index",
        });
      },
      fail: err => {
        wx.showToast({
          icon: 'none',
          title: '调用失败',
        })
        console.error('[云函数] [openapi] Add_fun 调用失败：', err)
      }
    })

  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    this.getdate()
    this.setData({
      zalue: '请选择'
    });
    try {
      var value = wx.getStorageSync('Login_time')
      if (value) {
        // Do something with return value
        wx.showToast({
          icon: 'none',
          title: '系统开小差去了，请联系管理员。',
        })
        that.setData({
          disabled: true,
        });
        wx.removeStorageSync('Login_time')
      }
    } catch (e) {
      // Do something when catch error
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
});
